import time

from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from common.Utils import TestDriver
from tests.LoginTest import LoginTest

# 测试问卷考试系统首页页面
class HomeTest:
    url = ""
    driver = ""

    def __init__(self):
        self.driver = TestDriver.driver
        self.url = "http://8.155.1.153:8081/home"
        self.driver.get(self.url)

    def HomeCheckElement(self):
        # 检查创建问卷按钮
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.ant-col.ant-col-xs-24.ant-col-sm-24.ant-col-md-24.ant-col-lg-24.ant-col-xl-8 > div > div.ant-card-body > div > a:nth-child(1)")
        # 检查创建考试按钮
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.ant-col.ant-col-xs-24.ant-col-sm-24.ant-col-md-24.ant-col-lg-24.ant-col-xl-8 > div > div.ant-card-body > div > a:nth-child(2)")
        # 检查首页标题元素
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-ghost > div.ant-page-header-heading > div")
        # 检查问卷数元素
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-ghost > div.ant-page-header-content > div > div > div > div > div > div.extraContent___3rMo7 > div:nth-child(1) > div > div.ant-statistic-title")
        # 检查考试数元素
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-ghost > div.ant-page-header-content > div > div > div > div > div > div.extraContent___3rMo7 > div:nth-child(2) > div > div.ant-statistic-title")
        # 检查我的考试按钮
        self.driver.find_element(By.CSS_SELECTOR, "#rc-tabs-0-tab-exam")
        # 检查我的问卷按钮
        self.driver.find_element(By.CSS_SELECTOR, "#rc-tabs-0-tab-survey")
        # 检查问卷记录按钮
        self.driver.find_element(By.CSS_SELECTOR, "#rc-tabs-0-tab-surveyHistory")
        # 检查考试记录按钮
        self.driver.find_element(By.CSS_SELECTOR, "#rc-tabs-0-tab-examHistory")

        TestDriver.getScreenshot()

    def CreateTest(self):
        # 测试创建问卷
        TestDriver.getScreenshot()
        # 记录问卷数
        prevNum = self.driver.find_element(By.CSS_SELECTOR,
                                           "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-ghost > div.ant-page-header-content > div > div > div > div > div > div.extraContent___3rMo7 > div:nth-child(1) > div > div.ant-statistic-content > span").text
        print(prevNum)
        # 点击创建问卷
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.ant-col.ant-col-xs-24.ant-col-sm-24.ant-col-md-24.ant-col-lg-24.ant-col-xl-8 > div > div.ant-card-body > div > a:nth-child(1)").click()
        time.sleep(3)
        # 点击新建一个单选题
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#rc-tabs-1-panel-1 > div > div > dl:nth-child(1) > div > div:nth-child(1) > dd > div").click()
        # 判断是否成功创建一个单选题
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#editorContent > div > div > div:nth-child(2) > div > div > div > div.q-content-wrap")
        time.sleep(1)
        # 点击保存按钮
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#editor > div.survey-main-panel > div.survey-main-panel-toolbar > div:nth-child(2) > div > button.ant-btn.ant-btn-primary.ant-btn-sm").click()

        # 返回上一个页面 检查问卷数变化
        self.driver.back()
        # 需要重新登录
        LoginTest().LoginSuccess()
        time.sleep(3)
        TestDriver.getScreenshot()
        curNum = self.driver.find_element(By.CSS_SELECTOR,
                                          "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-ghost > div.ant-page-header-content > div > div > div > div > div > div.extraContent___3rMo7 > div:nth-child(1) > div > div.ant-statistic-content > span > span").text
        print(curNum)
        assert (int(prevNum) + 1) == int(curNum)

        # 测试创建考试
        TestDriver.getScreenshot()
        # 记录考试数
        prev = self.driver.find_element(By.CSS_SELECTOR,
                                        "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-ghost > div.ant-page-header-content > div > div > div > div > div > div.extraContent___3rMo7 > div:nth-child(2) > div > div.ant-statistic-content > span > span").text
        print(prev)
        # 点击创建考试
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.ant-col.ant-col-xs-24.ant-col-sm-24.ant-col-md-24.ant-col-lg-24.ant-col-xl-8 > div > div.ant-card-body > div > a:nth-child(2)").click()
        time.sleep(3)
       # 点击新建一个单选题
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#rc-tabs-1-panel-1 > div > div > dl:nth-child(1) > div > div:nth-child(1) > dd > div").click()

        time.sleep(1)
        # 点击保存按钮
        self.driver.find_element(By.CSS_SELECTOR,
                                           "#editor > div.survey-main-panel > div.survey-main-panel-toolbar > div:nth-child(2) > div > button.ant-btn.ant-btn-primary.ant-btn-sm").click()
        # # 返回上一个页面
        self.driver.back()
        LoginTest().LoginSuccess()
        time.sleep(3)
        TestDriver.getScreenshot()

        cur = self.driver.find_element(By.CSS_SELECTOR,
                                       "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-ghost > div.ant-page-header-content > div > div > div > div > div > div.extraContent___3rMo7 > div:nth-child(2) > div > div.ant-statistic-content > span > span").text
        # 检查试卷数变化
        assert (int(prev) + 1) == int(cur)


